package com.yqb.study.common.schedule;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.yqb.study.common.rest.RestClient;
import com.yqb.study.common.vo.ResultVo;
import com.yqb.study.modular.persistence.model.City;
import com.yqb.study.util.DateUtil;
import com.yqb.study.util.ToolUtil;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class ScheduleJobs {
	
	// restful调用
	@Autowired
	private RestClient restClient;	
	
	// Restful服务对应的url地址  
    @Value("${springbootstudy.rest-service-path}")  
    private String restServicePath;	
	
	public final static long ONE_HOUR = 60 * 60 * 1000;

	// 本次任务执行完成后1分钟再次执行
	@Scheduled(fixedDelay = ONE_HOUR)
	public void fixedDelayJob() {		
		log.info("{}开始执行fixedDelayJob方法....", DateUtil.getTime());
		
		ResultVo<List<City>> resultVo = restClient.requestGet(restServicePath + "/data/restGetNoPara");
		if (ToolUtil.isNotEmpty(resultVo)) {
			log.info("{}完成执行fixedDelayJob方法....,执行结果是{}", DateUtil.getTime(), resultVo.toString());
		}			
	}

	// 每隔1分钟执行一次
	@Scheduled(fixedRate = ONE_HOUR)
	public void fixedRateJob() {
		log.info("{}开始执行fixedRateJob方法....", DateUtil.getTime());
		
		ResultVo<List<City>> resultVo = restClient.requestGet(restServicePath + "/data/restGetNoPara");
		if (ToolUtil.isNotEmpty(resultVo)) {
			log.info("{}完成执行fixedRateJob方法....,执行结果是{}", DateUtil.getTime(), resultVo.toString());
		}
	} 
	
	// 固定时间点执行
	@Scheduled(cron = "0 57 20 * * ?")
	public void cronJob() {
		log.info("{}开始执行cronJob方法....", DateUtil.getTime());
		
		ResultVo<List<City>> resultVo = restClient.requestGet(restServicePath + "/data/restGetNoPara");
		if (ToolUtil.isNotEmpty(resultVo)) {
			log.info("{}完成执行cronJob方法....,执行结果是{}", DateUtil.getTime(), resultVo.toString());
		}		
	}
}
